package com.ak.demo.service.impl;

import com.ak.demo.dao.UserMapper;
import com.ak.demo.entity.User;
import com.ak.demo.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    UserMapper userMapper;

    @Override
    public Boolean login(String username, String password, String email) {
        User user=new User(username,password,email);
        int result=userMapper.insert(user);
        return result>0;
    }

    @Override
    public Boolean email(String body, String to) {
        //创建邮件正文
        String from="203462009@qq.com";
        String subject="信息";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        try {
            mailSender.send(message);
            log.info("简单邮件已经发送。");
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送简单邮件时发生异常！", e);
        }
        return false;
    }
}
